import 'package:flutter_test/flutter_test.dart';
import 'package:flowplan_app/core/offline/offline_data_manager.dart';
import 'package:flowplan_app/domain/entities/note_request.dart';
import 'package:flowplan_app/domain/entities/project.dart';
import 'package:flowplan_app/domain/entities/todo.dart';

void main() {
  group('离线数据管理器测试', () {
    late OfflineDataManager offlineDataManager;

    setUp(() {
      offlineDataManager = OfflineDataManager();
    });

    tearDown(() async {
      // 清空所有离线操作记录
      await offlineDataManager.clearAllOperations();
    });

    group('保存和获取离线操作', () {
      test('保存离线操作记录', () async {
        // 准备测试数据
        final operation = OfflineOperation(
          id: 'test_operation_1',
          entityType: 'note',
          operationType: OfflineOperationType.create,
          data: {
            'title': '测试笔记',
            'content': '测试内容',
          },
          timestamp: DateTime.now(),
        );

        // 执行保存操作
        await offlineDataManager.saveOfflineOperation(operation);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperations();
        expect(operations, isNotEmpty);
        expect(operations.length, 1);
        expect(operations[0].id, 'test_operation_1');
        expect(operations[0].entityType, 'note');
        expect(operations[0].operationType, OfflineOperationType.create);
      });

      test('获取所有离线操作记录', () async {
        // 准备测试数据
        final operation1 = OfflineOperation(
          id: 'test_operation_1',
          entityType: 'note',
          operationType: OfflineOperationType.create,
          data: {'title': '笔记1'},
          timestamp: DateTime.now(),
        );

        final operation2 = OfflineOperation(
          id: 'test_operation_2',
          entityType: 'project',
          operationType: OfflineOperationType.update,
          data: {'projectName': '项目1'},
          timestamp: DateTime.now(),
        );

        // 保存操作记录
        await offlineDataManager.saveOfflineOperation(operation1);
        await offlineDataManager.saveOfflineOperation(operation2);

        // 获取所有操作记录
        final operations = await offlineDataManager.getOfflineOperations();
        expect(operations.length, 2);
      });

      test('根据实体类型获取离线操作记录', () async {
        // 准备测试数据
        final noteOperation = OfflineOperation(
          id: 'note_operation',
          entityType: 'note',
          operationType: OfflineOperationType.create,
          data: {'title': '笔记'},
          timestamp: DateTime.now(),
        );

        final projectOperation = OfflineOperation(
          id: 'project_operation',
          entityType: 'project',
          operationType: OfflineOperationType.create,
          data: {'projectName': '项目'},
          timestamp: DateTime.now(),
        );

        // 保存操作记录
        await offlineDataManager.saveOfflineOperation(noteOperation);
        await offlineDataManager.saveOfflineOperation(projectOperation);

        // 根据实体类型获取操作记录
        final noteOperations = await offlineDataManager.getOfflineOperationsByType('note');
        final projectOperations = await offlineDataManager.getOfflineOperationsByType('project');

        expect(noteOperations.length, 1);
        expect(noteOperations[0].entityType, 'note');
        expect(projectOperations.length, 1);
        expect(projectOperations[0].entityType, 'project');
      });
    });

    group('删除离线操作', () {
      test('删除已同步的操作记录', () async {
        // 准备测试数据
        final operation1 = OfflineOperation(
          id: 'operation_1',
          entityType: 'note',
          operationType: OfflineOperationType.create,
          data: {'title': '笔记1'},
          timestamp: DateTime.now(),
        );

        final operation2 = OfflineOperation(
          id: 'operation_2',
          entityType: 'project',
          operationType: OfflineOperationType.create,
          data: {'projectName': '项目1'},
          timestamp: DateTime.now(),
        );

        // 保存操作记录
        await offlineDataManager.saveOfflineOperation(operation1);
        await offlineDataManager.saveOfflineOperation(operation2);

        // 删除已同步的操作
        await offlineDataManager.removeSyncedOperations(['operation_1']);

        // 验证只剩下一个操作记录
        final operations = await offlineDataManager.getOfflineOperations();
        expect(operations.length, 1);
        expect(operations[0].id, 'operation_2');
      });

      test('清空所有离线操作记录', () async {
        // 准备测试数据
        final operation = OfflineOperation(
          id: 'test_operation',
          entityType: 'note',
          operationType: OfflineOperationType.create,
          data: {'title': '测试笔记'},
          timestamp: DateTime.now(),
        );

        // 保存操作记录
        await offlineDataManager.saveOfflineOperation(operation);

        // 验证操作已保存
        var operations = await offlineDataManager.getOfflineOperations();
        expect(operations, isNotEmpty);

        // 清空所有操作记录
        await offlineDataManager.clearAllOperations();

        // 验证操作已清空
        operations = await offlineDataManager.getOfflineOperations();
        expect(operations, isEmpty);
      });
    });

    group('笔记离线操作记录', () {
      test('创建笔记离线操作记录', () async {
        // 准备测试数据
        final request = NoteCreateRequest(
          title: '测试笔记',
          content: '测试内容',
          noteType: NoteType.general,
          tags: ['测试'],
          authorId: 1,
          isPublic: true,
        );

        // 创建离线操作记录
        await offlineDataManager.createNoteOfflineOperation(request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('note');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.create);
        expect(operations[0].data['title'], '测试笔记');
      });

      test('更新笔记离线操作记录', () async {
        // 准备测试数据
        final request = NoteUpdateRequest(
          title: '更新后的笔记',
          content: '更新后的内容',
          isPublic: false,
        );

        // 创建离线操作记录
        await offlineDataManager.updateNoteOfflineOperation(1, request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('note');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.update);
        expect(operations[0].data['id'], 1);
        expect(operations[0].data['title'], '更新后的笔记');
      });

      test('删除笔记离线操作记录', () async {
        // 创建离线操作记录
        await offlineDataManager.deleteNoteOfflineOperation(1);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('note');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.delete);
        expect(operations[0].data['id'], 1);
      });
    });

    group('项目离线操作记录', () {
      test('创建项目离线操作记录', () async {
        // 准备测试数据
        final request = ProjectCreateRequest(
          projectName: '测试项目',
          description: '测试项目描述',
          priority: Priority.high,
        );

        // 创建离线操作记录
        await offlineDataManager.createProjectOfflineOperation(request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('project');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.create);
        expect(operations[0].data['projectName'], '测试项目');
      });

      test('更新项目离线操作记录', () async {
        // 准备测试数据
        final request = ProjectUpdateRequest(
          projectName: '更新后的项目',
          status: ProjectStatus.inProgress,
        );

        // 创建离线操作记录
        await offlineDataManager.updateProjectOfflineOperation(1, request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('project');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.update);
        expect(operations[0].data['id'], 1);
        expect(operations[0].data['projectName'], '更新后的项目');
      });

      test('删除项目离线操作记录', () async {
        // 创建离线操作记录
        await offlineDataManager.deleteProjectOfflineOperation(1);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('project');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.delete);
        expect(operations[0].data['id'], 1);
      });
    });

    group('待办离线操作记录', () {
      test('创建待办离线操作记录', () async {
        // 准备测试数据
        final request = TodoCreateRequest(
          projectId: 1,
          title: '测试任务',
          todoType: TodoType.develop,
          priority: Priority.high,
        );

        // 创建离线操作记录
        await offlineDataManager.createTodoOfflineOperation(request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('todo');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.create);
        expect(operations[0].data['title'], '测试任务');
      });

      test('更新待办离线操作记录', () async {
        // 准备测试数据
        final request = TodoUpdateRequest(
          title: '更新后的任务',
          status: TodoStatus.inProgress,
        );

        // 创建离线操作记录
        await offlineDataManager.updateTodoOfflineOperation(1, request);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('todo');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.update);
        expect(operations[0].data['id'], 1);
        expect(operations[0].data['title'], '更新后的任务');
      });

      test('删除待办离线操作记录', () async {
        // 创建离线操作记录
        await offlineDataManager.deleteTodoOfflineOperation(1);

        // 验证操作已保存
        final operations = await offlineDataManager.getOfflineOperationsByType('todo');
        expect(operations, isNotEmpty);
        expect(operations[0].operationType, OfflineOperationType.delete);
        expect(operations[0].data['id'], 1);
      });
    });
  });
}